package com.huarui.part.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.ewaf.framework.action.BaseAction;

import com.huarui.params.Params;
import com.huarui.part.model.PartCategory;
import com.huarui.part.service.PartService;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;

public class CategoryAction extends BaseAction {
	private static final long serialVersionUID = 2924526709280705961L;

	private PartService partService;

	private PartCategory category;

	private int level1Id;

	private Map<String, Object> resultMap = new HashMap<String, Object>();

	private Map<String, Object> mesMap;

	private String queryParam;

	private Integer start;

	private Integer id;

	public String getQueryParam() {
		return queryParam;
	}

	public void setQueryParam(String queryParam) {
		this.queryParam = queryParam;
	}

	public Integer getStart() {
		return start;
	}

	public void setStart(Integer start) {
		this.start = start;
	}

	public Map<String, Object> getMesMap() {
		return mesMap;
	}

	public void setMesMap(Map<String, Object> mesMap) {
		this.mesMap = mesMap;
	}

	@Validations(requiredStrings = { @RequiredStringValidator(fieldName = "category.level1Name", key = "category.level1Name.required") })
	public String doAdd() {

		partService.createPartCategory(category);

		return SUCCESS;
	}

	@Validations(requiredStrings = {
			@RequiredStringValidator(fieldName = "category.level2Name", key = "category.level2Name.required"),
			@RequiredStringValidator(fieldName = "category.level1Id", key = "category.level1Id.required") })
	public String doAddSub() {

		PartCategory category1 = partService.getCategoryById(category
				.getLevel1Id());
		category.setLevel1Name(category1.getLevel1Name());
		partService.createPartCategory(category);

		return SUCCESS;
	}

	public String toModi() {

		category = partService.getCategoryById(category.getId());

		return "toModi";
	}

	@Validations(requiredStrings = { @RequiredStringValidator(fieldName = "category.level1Name", key = "category.level1Name.required") })
	public String doModi() {

		partService.updateCategory(category);
		return SUCCESS;
	}

	/**
	 * 得到所有一级类别
	 * 
	 * @return
	 */
	public String getFirstCategorys() {

		List<PartCategory> categorys = partService.getCategoryLevelList();

		JSONArray root = JSONArray.fromObject(categorys);

		resultMap.put("root", root);

		return JSON;
	}

	/**
	 * 根据一级类别level1Id得到两级级别
	 * 
	 * @return
	 * @throws IOException
	 */
	public String getSubCategoryList() {

		List<PartCategory> categorys = partService.getSubLevel(level1Id);

		JSONArray root = JSONArray.fromObject(categorys);

		resultMap.put("root", root);

		return JSON;
	}

	public String getSubCategoryListById() {

		List<PartCategory> categorys = partService.getSubLevelById(id);

		JSONArray root = JSONArray.fromObject(categorys);

		resultMap.put("root", root);
		resultMap.put("success", true);
		return JSON;
	}

	public Map<String, Object> getResultMap() {
		return resultMap;
	}

	public PartCategory getCategory() {
		return category;
	}

	public void setCategory(PartCategory category) {
		this.category = category;
	}

	public void setPartService(PartService partService) {
		this.partService = partService;
	}

	public int getLevel1Id() {
		return level1Id;
	}

	public void setLevel1Id(int level1Id) {
		this.level1Id = level1Id;
	}

	/**
	 * 后台查询
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String listPartCategory() {
		Map<String, Object> paramsMap = null;
		if (queryParam == null) {
			paramsMap = new HashMap<String, Object>();
		} else {
			JSONObject jo = JSONObject.fromObject(queryParam);
			paramsMap = (Map<String, Object>) JSONObject.toBean(jo,
					HashMap.class);
		}
		paramsMap.put("start", start == null ? 0 : start);
		paramsMap.put("pageSize", Params.BACKGROUND_PAGE_SIZE);
		mesMap = this.partService.getPartCategoryListByComplex(paramsMap);
		return SUCCESS;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

}
